<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>一些JS骚操作 | sunJsona的个人博客</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="一、强制类型转换1.1 string强制转换为数字12345678910111213//可以用*1来转化为数字((实际上是调用.valueOf方法) 然后使用Number.isNaN来判断是否为NaN，或者使用 a !== a 来判断是否为NaN)&amp;apos;32&amp;apos; * 1            // 32&amp;apos;ds&amp;apos; * 1            // NaNnull">
<meta property="og:type" content="article">
<meta property="og:title" content="一些JS骚操作">
<meta property="og:url" content="https://xiaochangzai.github.io/2020/07/07/一些JS骚操作/index.html">
<meta property="og:site_name" content="sunJsona的个人博客">
<meta property="og:description" content="一、强制类型转换1.1 string强制转换为数字12345678910111213//可以用*1来转化为数字((实际上是调用.valueOf方法) 然后使用Number.isNaN来判断是否为NaN，或者使用 a !== a 来判断是否为NaN)&amp;apos;32&amp;apos; * 1            // 32&amp;apos;ds&amp;apos; * 1            // NaNnull">
<meta property="og:locale" content="Simple Chinese">
<meta property="og:updated_time" content="2020-07-07T05:00:13.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="一些JS骚操作">
<meta name="twitter:description" content="一、强制类型转换1.1 string强制转换为数字12345678910111213//可以用*1来转化为数字((实际上是调用.valueOf方法) 然后使用Number.isNaN来判断是否为NaN，或者使用 a !== a 来判断是否为NaN)&amp;apos;32&amp;apos; * 1            // 32&amp;apos;ds&amp;apos; * 1            // NaNnull">
  
    <link rel="alternative" href="/atom.xml" title="sunJsona的个人博客" type="application/atom+xml">
  
  
    <link rel="icon" href="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg">
  
  <link rel="stylesheet" href="/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">sunJsona</a></h1>
		</hgroup>

		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/">主页</a></li>
				        
							<li><a href="/archives">所有文章</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">sunJsona</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">sunJsona</h1>
			</hgroup>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/">主页</a></li>
		        
					<li><a href="/archives">所有文章</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-一些JS骚操作" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2020/07/07/一些JS骚操作/" class="article-date">
  	<time datetime="2020-07-07T05:00:13.000Z" itemprop="datePublished">2020-07-07</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      一些JS骚操作
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="一、强制类型转换"><a href="#一、强制类型转换" class="headerlink" title="一、强制类型转换"></a>一、强制类型转换</h2><h5 id="1-1-string强制转换为数字"><a href="#1-1-string强制转换为数字" class="headerlink" title="1.1 string强制转换为数字"></a>1.1 string强制转换为数字</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">//可以用*1来转化为数字((实际上是调用.valueOf方法) 然后使用Number.isNaN来判断是否为NaN，或者使用 a !== a 来判断是否为NaN)</span><br><span class="line">&apos;32&apos; * 1            // 32</span><br><span class="line">&apos;ds&apos; * 1            // NaN</span><br><span class="line">null * 1            // 0</span><br><span class="line">undefined * 1    // NaN</span><br><span class="line">1  * &#123; valueOf: ()=&gt;&apos;3&apos; &#125;        // 3</span><br><span class="line">//使用+来转化字符串为数字</span><br><span class="line">+ &apos;123&apos;            // 123</span><br><span class="line">+ &apos;ds&apos;               // NaN</span><br><span class="line">+ &apos;&apos;                    // 0</span><br><span class="line">+ null              // 0</span><br><span class="line">+ undefined    // NaN</span><br><span class="line">+ &#123; valueOf: ()=&gt;&apos;3&apos; &#125;    // 3</span><br></pre></td></tr></table></figure>
<h5 id="1-2-使用Boolean过滤数组中的所有假值"><a href="#1-2-使用Boolean过滤数组中的所有假值" class="headerlink" title="1.2 使用Boolean过滤数组中的所有假值"></a>1.2 使用Boolean过滤数组中的所有假值</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const compact = arr =&gt; arr.filter(Boolean)</span><br><span class="line">compact([0, 1, false, 2, &apos;&apos;, 3, &apos;a&apos;, &apos;e&apos; * 23, NaN, &apos;s&apos;, 34])</span><br></pre></td></tr></table></figure>
<h5 id="1-3-数值取整-–去除小数点后面的值"><a href="#1-3-数值取整-–去除小数点后面的值" class="headerlink" title="1.3 数值取整 –去除小数点后面的值"></a>1.3 数值取整 –去除小数点后面的值</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">~~2.33</span><br><span class="line">2.33 | 0</span><br><span class="line">2.33 &gt;&gt; 0</span><br><span class="line">//Math.floor()向下取整,值永远只会变小</span><br></pre></td></tr></table></figure>
<h5 id="1-4-判断奇偶数-负数同样适用"><a href="#1-4-判断奇偶数-负数同样适用" class="headerlink" title="1.4 判断奇偶数,负数同样适用"></a>1.4 判断奇偶数,负数同样适用</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const num=3;</span><br><span class="line">!!(num &amp; 1)                    // true</span><br><span class="line">!!(num % 2)                    // true</span><br><span class="line">//以上两种形式返回true的都是奇数</span><br></pre></td></tr></table></figure>
<h5 id="JS-amp-amp-妙用-多重if-else-选择情况"><a href="#JS-amp-amp-妙用-多重if-else-选择情况" class="headerlink" title="JS|| &amp;&amp; 妙用 多重if else 选择情况"></a>JS|| &amp;&amp; 妙用 多重if else 选择情况</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">假设对成长速度显示规定如下:</span><br><span class="line">成长速度为5显示1个箭头</span><br><span class="line">成长速度为10显示2个箭头</span><br><span class="line">成长速度为12显示3个箭头</span><br><span class="line">成长速度为15显示4个箭头</span><br><span class="line">其他显示为0个箭头</span><br><span class="line">*/</span><br><span class="line">//一般代码</span><br><span class="line">var add_level = 0; </span><br><span class="line">if(add_step == 5)&#123; </span><br><span class="line">add_level = 1; </span><br><span class="line">&#125; </span><br><span class="line">else if(add_step == 10)&#123; </span><br><span class="line">add_level = 2; </span><br><span class="line">&#125; </span><br><span class="line">else if(add_step == 12)&#123; </span><br><span class="line">add_level = 3; </span><br><span class="line">&#125; </span><br><span class="line">else if(add_step == 15)&#123; </span><br><span class="line">add_level = 4; </span><br><span class="line">&#125; </span><br><span class="line">else &#123; </span><br><span class="line">add_level = 0; </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">//好一点的switch</span><br><span class="line">var add_level = 0; </span><br><span class="line">switch(add_step)&#123; </span><br><span class="line">    case 5 : add_level = 1; </span><br><span class="line">    break; </span><br><span class="line">    case 10 : add_level = 2; </span><br><span class="line">    break; </span><br><span class="line">    case 12 : add_level = 3; </span><br><span class="line">    break; </span><br><span class="line">    case 15 : add_level = 4; </span><br><span class="line">    break; </span><br><span class="line">    default : add_level = 0; </span><br><span class="line">    break; </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//更好一点的</span><br><span class="line">var add_level = (add_step==5 &amp;&amp; 1) || (add_step==10 &amp;&amp; 2) || (add_step==12 &amp;&amp; 3) || (add_step==15 &amp;&amp; 4) || 0; </span><br><span class="line"></span><br><span class="line">//还有更好的</span><br><span class="line">var add_level=&#123;&apos;5&apos;:1,&apos;10&apos;:2,&apos;12&apos;:3,&apos;15&apos;:4&#125;[add_step] || 0;</span><br></pre></td></tr></table></figure>
<h2 id="二、函数"><a href="#二、函数" class="headerlink" title="二、函数"></a>二、函数</h2><h5 id="2-1-惰性载入函数"><a href="#2-1-惰性载入函数" class="headerlink" title="2.1 惰性载入函数"></a>2.1 惰性载入函数</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">//这个判断依据在整个项目运行期间一般不会变化，所以判断分支在整个项目运行期间只会运行某个特定分支，那么就可以考虑惰性载入函数</span><br><span class="line">function foo()&#123;</span><br><span class="line">    if(a !== b)&#123;</span><br><span class="line">        console.log(&apos;aaa&apos;)</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        console.log(&apos;bbb&apos;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 优化后</span><br><span class="line">function foo()&#123;</span><br><span class="line">    if(a != b)&#123;</span><br><span class="line">        foo = function()&#123;</span><br><span class="line">            console.log(&apos;aaa&apos;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        foo = function()&#123;</span><br><span class="line">            console.log(&apos;bbb&apos;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return foo();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="2-2-动态添加js"><a href="#2-2-动态添加js" class="headerlink" title="2.2 动态添加js"></a>2.2 动态添加js</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//动态添加js</span></span><br><span class="line"><span class="built_in">document</span>.write(<span class="string">"&lt;script src='"</span> + context.path + <span class="string">"/resource/apps/logger.js'&gt;&lt;/script&gt;"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 动态添加JS</span></span><br><span class="line"><span class="comment"> * @param &#123;Object&#125; js</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">loadJs</span>(<span class="params">js</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> s = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">    s.setAttribute(<span class="string">'type'</span>, <span class="string">'text/javascript'</span>);</span><br><span class="line">    s.setAttribute(<span class="string">'src'</span>, js);</span><br><span class="line">    <span class="keyword">var</span> content = <span class="built_in">document</span>.getElementById(<span class="string">"container-fluid"</span>);</span><br><span class="line">    content.appendChild(s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="三、数组"><a href="#三、数组" class="headerlink" title="三、数组"></a>三、数组</h2><h5 id="3-1-reduce方法同时实现map和filter"><a href="#3-1-reduce方法同时实现map和filter" class="headerlink" title="3.1 reduce方法同时实现map和filter"></a>3.1 reduce方法同时实现map和filter</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numbers = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>];</span><br><span class="line"><span class="keyword">const</span> doubledOver50 = numbers.reduce(<span class="function">(<span class="params">finalList, num,currentIndex,numbers</span>) =&gt;</span> &#123;</span><br><span class="line">  </span><br><span class="line">  <span class="built_in">console</span>.log(finalList);</span><br><span class="line">  <span class="built_in">console</span>.log(num);</span><br><span class="line">  <span class="built_in">console</span>.log(currentIndex);</span><br><span class="line">  <span class="built_in">console</span>.log(numbers);</span><br><span class="line">  num = num * <span class="number">2</span>;</span><br><span class="line">  <span class="keyword">if</span> (num &gt; <span class="number">50</span>) &#123;</span><br><span class="line">    finalList.push(num);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> finalList;</span><br><span class="line">&#125;, []);</span><br></pre></td></tr></table></figure>
<h5 id="五种方法实现值交换"><a href="#五种方法实现值交换" class="headerlink" title="五种方法实现值交换"></a>五种方法实现值交换</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="number">1.</span> <span class="keyword">var</span> temp = a; a = b; b = temp; <span class="comment">//(传统，但需要借助临时变量)</span></span><br><span class="line"> </span><br><span class="line"><span class="number">2.</span> a ^= b; b ^= a; a ^= b; <span class="comment">//(需要两个整数)</span></span><br><span class="line"> </span><br><span class="line"><span class="number">3.</span> b = [a, a = b][<span class="number">0</span>] <span class="comment">//(借助数组)</span></span><br><span class="line"> </span><br><span class="line"><span class="number">4.</span> [a, b] = [b, a]; <span class="comment">//(ES6，解构赋值)</span></span><br><span class="line"> </span><br><span class="line"><span class="number">5.</span> a = a + b; b = a - b; a = a - b; <span class="comment">//(小学奥赛题)</span></span><br></pre></td></tr></table></figure>
<h5 id="去掉小数部分"><a href="#去掉小数部分" class="headerlink" title="去掉小数部分"></a>去掉小数部分</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">parseInt(num)</span><br><span class="line"> </span><br><span class="line">~~num</span><br><span class="line"> </span><br><span class="line">num &gt;&gt; 0</span><br><span class="line"> </span><br><span class="line">num | 0</span><br></pre></td></tr></table></figure>
<h5 id="判断x是否是整数"><a href="#判断x是否是整数" class="headerlink" title="判断x是否是整数"></a>判断x是否是整数</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">function isInt(x) &#123;</span><br><span class="line"> </span><br><span class="line">  return (x ^ 0) === x</span><br><span class="line"> </span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">// return Math.round(x) === x</span><br><span class="line"> </span><br><span class="line">// return (typeof x === &apos;number&apos;) &amp;&amp; (x % 1 === 0)</span><br><span class="line"> </span><br><span class="line">// ES6 -&gt; Number.isInteger()</span><br></pre></td></tr></table></figure>
<h5 id="数组去重"><a href="#数组去重" class="headerlink" title="数组去重"></a>数组去重</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">// ES6</span><br><span class="line"> </span><br><span class="line">Array.from(new Set(arr))</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">// ES5</span><br><span class="line"> </span><br><span class="line">arr.filter(function(ele, index, array)&#123;</span><br><span class="line"> </span><br><span class="line">    return index===array.indexOf(ele)</span><br><span class="line"> </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h5 id="数组最大值"><a href="#数组最大值" class="headerlink" title="数组最大值"></a>数组最大值</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function maxArr(arr) &#123;</span><br><span class="line"> </span><br><span class="line"> return Math.max.apply(null, arr)</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="数组最小值"><a href="#数组最小值" class="headerlink" title="数组最小值"></a>数组最小值</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function minArr(arr) &#123;</span><br><span class="line"> </span><br><span class="line"> return Math.min.apply(null, arr)</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="随机获取数组的一个成员"><a href="#随机获取数组的一个成员" class="headerlink" title="随机获取数组的一个成员"></a>随机获取数组的一个成员</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function randomOne(arr) &#123;</span><br><span class="line"> </span><br><span class="line"> return arr[Math.floor(Math.random() * arr.length)]</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="产生随机颜色"><a href="#产生随机颜色" class="headerlink" title="产生随机颜色"></a>产生随机颜色</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function getRandomColor() &#123;</span><br><span class="line"> </span><br><span class="line">    return `#$&#123;Math.random().toString(16).substr(2, 6)&#125;`</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="随机生成指定长度字符串"><a href="#随机生成指定长度字符串" class="headerlink" title="随机生成指定长度字符串"></a>随机生成指定长度字符串</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">function randomStr(n) &#123;</span><br><span class="line">    let standard = &apos;abcdefghijklmnopqrstuvwxyz9876543210&apos;</span><br><span class="line">    let len = standard.length</span><br><span class="line">    let result = &apos;&apos;</span><br><span class="line"></span><br><span class="line">    for (let i = 0; i &lt; n; i++) &#123;</span><br><span class="line">        result += standard.charAt(Math.floor(Math.random() * len))</span><br><span class="line">  &#125;</span><br><span class="line">  return result</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="深拷贝"><a href="#深拷贝" class="headerlink" title="深拷贝"></a>深拷贝</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">JSON.parse(JSON.stringify(obj))</span><br></pre></td></tr></table></figure>
<p>##### </p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/07/07/only-child 用法 React click事件/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          only-child 用法 React click事件
        
      </div>
    </a>
  
  
    <a href="/2020/07/07/公司内部笔记/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title"></div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="一些JS骚操作" data-title="一些JS骚操作" data-url="https://xiaochangzai.github.io/2020/07/07/一些JS骚操作/"  data-images="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg" data-content="一些JS骚操作">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2022 sunJsona
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>